package com.shycloud.mido.opter.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shycloud.mido.common.core.constant.CommonConstants;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.opter.api.dto.UDIDSearchDTO;
import com.shycloud.mido.opter.api.entity.StockDeviceInfo;
import com.shycloud.mido.opter.mapper.StockDeviceInfoMapper;
import com.shycloud.mido.opter.service.StockOrderService;

import org.springframework.web.bind.annotation.*;

import lombok.AllArgsConstructor;

import java.util.Map;

@RestController
@AllArgsConstructor
@RequestMapping("/udid")
public class UDIDController {

	private StockDeviceInfoMapper stockDeviceInfoMapper;
	private StockOrderService orderService;

	/**
	 * 后台管理系统 - 商品分配管理列表接口
	 *
	 * @param current 当前页
	 * @param size    每页显示条数
	 * @param udid    设备码
	 * @param status  是否激活 0-未激活 1-已激活
	 * @return
	 * @author nianhua.jiang
	 * @date 2021/1/22 9:44
	 **/
	@GetMapping("/getUDIDPage")
	public R getUDIDPage(Integer current, Integer size, String udid, String status) {

		Page<StockDeviceInfo> page = new Page<>(current, size);
		UDIDSearchDTO dto = new UDIDSearchDTO();
		//设备码
		dto.setUdid(udid);
		//状态
		dto.setStatus(status);
		return new R<>(stockDeviceInfoMapper.getUDIDPage(page, dto));
	}

	@GetMapping("/addUDID")
	public R addUDID(Integer count) {
		return new R<>(orderService.genUDID("2", count));
	}

	@PostMapping("updUdid")
	public R updUdid(@RequestBody Map<String, Object> reqMap) {

		String udid = (String) reqMap.get("udid");

		Integer optId = (Integer) reqMap.get("optId");

		StockDeviceInfo stockDeviceInfo = stockDeviceInfoMapper.selectById(udid);

		if (stockDeviceInfo == null) {
			return new R(CommonConstants.FAIL, "UDID不存在", false);
		}

		stockDeviceInfo.setOptId(optId);

		stockDeviceInfoMapper.updateById(stockDeviceInfo);

		return new R(true);

	}

	@GetMapping("getUdidDetail")
	public R checkDevOut(String udid) {
		return new R(stockDeviceInfoMapper.selectById(udid));
	}

}
